package cn.dada.dadarecipes.user.shipping_address.service.impl;

import cn.dada.dadarecipes.base.exception.ServiceException;
import cn.dada.dadarecipes.base.response.StatusCode;
import cn.dada.dadarecipes.user.shipping_address.mapper.ShippingAddressMapper;
import cn.dada.dadarecipes.user.shipping_address.pojo.dto.ShippingAddressParam;
import cn.dada.dadarecipes.user.shipping_address.pojo.entity.ShippingAddress;
import cn.dada.dadarecipes.user.shipping_address.pojo.vo.ShippingAddressVO;
import cn.dada.dadarecipes.user.shipping_address.service.ShippingAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.util.locale.provider.LocaleServiceProviderPool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class ShippingAddressServiceImpl implements ShippingAddressService {

    @Autowired
    ShippingAddressMapper shippingAddressMapper;
    @Override
    public void insertAddress(ShippingAddressParam shippingAddressParam) {
        log.debug("用户新增地址:shippingAddressParam={}", shippingAddressParam);
        // 创建一个新的ShippingAddress对象
        ShippingAddress shippingAddress = new ShippingAddress();
        // 使用BeanUtils工具类将参数对象的属性复制到新的ShippingAddress对象中
        BeanUtils.copyProperties(shippingAddressParam, shippingAddress);
        // 判断是否为新增地址或更新地址
        if(shippingAddress.getId()==null){
            // 如果ID为null，表示这是一个新的地址，调用Mapper层的insert方法插入新地址
            shippingAddressMapper.insert(shippingAddress);
        }else {
            // 如果ID不为null，表示这是更新现有地址，调用Mapper层的update方法更新地址
            shippingAddressMapper.update(shippingAddress);
        }

    }

    @Override
    public void deleteAddress(Long id) {
        log.debug("用户删除地址:id={}", id);
        // 调用Mapper层的delete方法删除指定ID的地址
        shippingAddressMapper.delete(id);
    }


    @Override
    public List<ShippingAddressVO> query(ShippingAddressParam shippingAddressParam) {
        log.debug("用户查询地址:shippingAddressParam{}", shippingAddressParam);
        Long userId = shippingAddressParam.getUserId();
        Long id = shippingAddressParam.getId();
        if(id == null){
            log.debug("用户查询地址:userId={}", userId);
            List<ShippingAddressVO> shippingAddressVOS = shippingAddressMapper.selectShippingAddress(userId);
            if(shippingAddressVOS.isEmpty()){
                log.error("地址查询失败: userId={}", userId);
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
            return shippingAddressVOS;

        }
        ShippingAddressVO shippingAddressVO = shippingAddressMapper.selectShippingAddressById(userId,id);
        ArrayList<ShippingAddressVO> shippingAddressVOS = new ArrayList<>();
        shippingAddressVOS.add(shippingAddressVO);
        if (shippingAddressVO == null) {
            log.error("地址查询失败: userId={}, id={}", userId, id);
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
        return shippingAddressVOS;
    }
}
